﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;
namespace DAL
{
    public class AdDAL
    {
        public static int Save(AdInfo info)
        {
            string sql = string.Format("insert into [ad]([Name],[Img],[Link],[Position],[BeginDate],[EndDate],[CreateDate]) values(@Name,@Img,@Link,@Position,@BeginDate,@EndDate,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Link", info.Link),
                new SqlParameter("@Position", info.Position),
                new SqlParameter("@BeginDate", info.BeginDate),
                new SqlParameter("@EndDate", info.EndDate),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(AdInfo info)
        {
            string sql = string.Format("update [ad] set [Name]=@Name,[Img]=@Img,[Link]=@Link,[Position]=@Position,[BeginDate]=@BeginDate,[EndDate]=@EndDate where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Img", info.Img),
                new SqlParameter("@Link", info.Link),
                new SqlParameter("@Position", info.Position),
                new SqlParameter("@BeginDate", info.BeginDate),
                new SqlParameter("@EndDate", info.EndDate),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static List<AdInfo> FindTop(int top, int position)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top {0} * from [ad] where Position = {1} and getdate() between BeginDate and EndDate order by ID desc",top, position));
            List<AdInfo> list = new List<AdInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row));
            }
            return list;
        }
        public static AdInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[ad]");
            return Simple_Data(dr);
        }
        public static List<AdInfo> FindPage(string keyword, string begindate,string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [ad].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [ad].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[ad]", out count);
            List<AdInfo> list = new List<AdInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(All_Data(row));
            }
            return list;
        }
        
        private static AdInfo Simple_Data(DataRow dr)
        {
            AdInfo info = new AdInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Img = DataConvert.ToString(dr["Img"]);
                info.Link = DataConvert.ToString(dr["Link"]);
                info.Position = DataConvert.ToInt(dr["Position"]);
                info.BeginDate = DataConvert.ToDateTime(dr["BeginDate"]);
                info.EndDate = DataConvert.ToDateTime(dr["EndDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.PositionText = Commons.Position[info.Position];
            }
            return info;
        }

        private static AdInfo All_Data(DataRow dr)
        {
            AdInfo info = new AdInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Img = Commons.domain + DataConvert.ToString(dr["Img"]);
                info.Link = DataConvert.ToString(dr["Link"]);
                info.Position = DataConvert.ToInt(dr["Position"]);
                info.BeginDate = DataConvert.ToDateTime(dr["BeginDate"]);
                info.EndDate = DataConvert.ToDateTime(dr["EndDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.PositionText = Commons.Position[info.Position];
            }
            return info;
        }
    }
}
